package org.example.Config;

import org.opencv.core.Core;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;

public class OpenCVLoader {
    public static void loadOpenCVLibrary() {
        try {
            // 获取 OpenCV 库文件的路径
            String libraryName = Core.NATIVE_LIBRARY_NAME;
            Path resourceDirectory = Paths.get("src", "main", "resources");
            try (Stream<Path> paths = Files.walk(resourceDirectory)) {
                paths.filter(Files::isRegularFile)
                        .forEach(filePath -> {
                            if (filePath.getFileName().toString().contains(libraryName)) {
                                try {
                                    // 将库文件复制到临时目录
                                    Path tempFile = Files.createTempFile(null, null);
                                    Files.copy(filePath, tempFile);
                                    System.load(tempFile.toAbsolutePath().toString());
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        });
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}